package com.fulin.thread.queue.delayqueue;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
*
* @description 
* @auther fulin16@crnet.com.hk
* @since 2019年4月13日 下午2:48:23
*
*/
public class Message implements Delayed{
	
	private int id;
	private String body;  //消息体
	private long delayTime; //延迟时间
	
	public String getBody(){
		return this.body;
	}
	
	public Message(int id,String body,long delayTime){
		this.id = id;
		this.body = body +" " +  time();
		this.delayTime = TimeUnit.MILLISECONDS.convert(System.currentTimeMillis() + delayTime, TimeUnit.MILLISECONDS);
	}
	
	@Override
	public int compareTo(Delayed o) {
//		Message message = (Message) o;
		if(this.getDelay(TimeUnit.MILLISECONDS) > o.getDelay(TimeUnit.MILLISECONDS)) return 1;
		if(this.getDelay(TimeUnit.MILLISECONDS) < o.getDelay(TimeUnit.MILLISECONDS)) return -1;
		return 0;
	}

	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert((this.delayTime - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
	}

	
	public static String time(){
		LocalDateTime ldt = LocalDateTime.now();
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
		String format = ldt.format(dtf);
		return format;
	}
}
